import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import jpl.Atom;
import jpl.Query;
import jpl.Term;

/**
 * 
 */

/**
 * @author "Cunxin Jia"
 *
 */
public class SWIProver {
	
	private Set<Literal> covered;
	private Set<Literal> uncovered;
	
	private String fileName;
	private Set<Literal> set;
	
	public SWIProver (String fileName, Set<Literal> set) {
		this.fileName = fileName;
		this.set = set;
	}
	public static void main(String[] args) {
		String fileName = "test.pl";
		String query = "daughter(susan,tom).";
		System.out.println(executeQuery(fileName,query));
	}
	
	public Set<Literal> getCoveredSet() {
		return covered;
	}
	
	public Set<Literal> getUncovered() {
		return uncovered;
	}
	
	public int executeQuery() {
		Term[] t = new Term[1];
		t[0] = new Atom(fileName);
		Query load = new Query("consult", t);
		if(!load.hasSolution()) {
			return 0;
		}
		covered = new HashSet<Literal>();
		uncovered = new HashSet<Literal>();
		Iterator<Literal> iter = set.iterator();
		int coveredPosNum = 0;
		while(iter.hasNext()) {
			Literal curLiteral = iter.next();
			Query query = new Query(curLiteral.toString());
			if(query.hasSolution()) {
				coveredPosNum++;
				covered.add(curLiteral);
			}
			else {
				uncovered.add(curLiteral);
			}
		}
		return coveredPosNum;
	}
	
	public static boolean executeQuery(String loadFileName, String query) {
		Term[] t = new Term[1];
		t[0] = new Atom(loadFileName);
		Query load = new Query("consult",t);
		if(!load.hasSolution()){
			System.out.println("There is some problem in the file " + loadFileName);
			return false; //actually it should exit
		}
		Query q = new Query(query);
		if(q.hasSolution())
			return true;
		load.close();
		q.close();
		return false;
	}
}
